//package com.ivo.act.config;
//
//import org.activiti.spring.SpringAsyncExecutor;
//import org.activiti.spring.SpringCallerRunsRejectedJobsHandler;
//import org.activiti.spring.SpringProcessEngineConfiguration;
//import org.activiti.spring.SpringRejectedJobsHandler;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.support.ResourcePatternResolver;
//import org.springframework.core.task.SimpleAsyncTaskExecutor;
//import org.springframework.core.task.TaskExecutor;
//import org.springframework.transaction.PlatformTransactionManager;
//import org.springframework.util.StringUtils;
//
//import javax.annotation.Resource;
//import javax.sql.DataSource;
//import java.io.IOException;
//import java.util.*;
//
///**
// * 签核引擎配置
// * @author wangjian
// */
//@Configuration
//@EnableConfigurationProperties(ActivitiProperties.class)
//public class ProcessEngineConfiguration extends AbstractProcessEngineConfiguration {
//
//    private static final Logger logger = LoggerFactory.getLogger(ProcessEngineConfiguration.class);
//
//    @Resource
//    protected ActivitiProperties activitiProperties;
//
//    @Resource
//    private PlatformTransactionManager transactionManager;
//
//    @Resource
//    private DataSource dataSource;
//
//    @Resource
//    private ResourcePatternResolver resourceLoader;
//
//    @Bean
//    public SpringProcessEngineConfiguration springProcessEngineConfiguration(SpringAsyncExecutor springAsyncExecutor) throws IOException {
//        List<org.springframework.core.io.Resource> procDefResources = discoverProcessDefinitionResources(
//                this.resourceLoader, this.activitiProperties.getProcessDefinitionLocationPrefix(),
//                this.activitiProperties.getProcessDefinitionLocationSuffixes(),
//                this.activitiProperties.isCheckProcessDefinitions());
//
//        SpringProcessEngineConfiguration conf = new SpringProcessEngineConfiguration();
//        conf.setDataSource(dataSource);
//        conf.setTransactionManager(transactionManager);
//
//        conf.setDeploymentName(defaultText(activitiProperties.getDeploymentName(), conf.getDeploymentName()));
//        conf.setDatabaseSchema(defaultText(activitiProperties.getDatabaseSchema(), conf.getDatabaseSchema()));
//        conf.setDatabaseSchemaUpdate(defaultText(activitiProperties.getDatabaseSchemaUpdate(), conf.getDatabaseSchemaUpdate()));
//
//        conf.setDbIdentityUsed(activitiProperties.isDbIdentityUsed());
//        conf.setDbHistoryUsed(activitiProperties.isDbHistoryUsed());
//
//        conf.setAsyncExecutorActivate(activitiProperties.isAsyncExecutorActivate());
//
//        conf.setMailServerHost(activitiProperties.getMailServerHost());
//        conf.setMailServerPort(activitiProperties.getMailServerPort());
//        conf.setMailServerUsername(activitiProperties.getMailServerUserName());
//        conf.setMailServerPassword(activitiProperties.getMailServerPassword());
//        conf.setMailServerDefaultFrom(activitiProperties.getMailServerDefaultFrom());
//        conf.setMailServerUseSSL(activitiProperties.isMailServerUseSsl());
//        conf.setMailServerUseTLS(activitiProperties.isMailServerUseTls());
//
//        conf.setHistoryLevel(activitiProperties.getHistoryLevel());
//
//        if (activitiProperties.getCustomMybatisMappers() != null) {
//            conf.setCustomMybatisMappers(getCustomMybatisMapperClasses(activitiProperties.getCustomMybatisMappers()));
//        }
//
//        if (activitiProperties.getCustomMybatisXMLMappers() != null) {
//            conf.setCustomMybatisXMLMappers(new HashSet<>(activitiProperties.getCustomMybatisXMLMappers()));
//        }
//
//        if (activitiProperties.getCustomMybatisMappers() != null) {
//            conf.setCustomMybatisMappers(getCustomMybatisMapperClasses(activitiProperties.getCustomMybatisMappers()));
//        }
//
//        if (activitiProperties.getCustomMybatisXMLMappers() != null) {
//            conf.setCustomMybatisXMLMappers(new HashSet<>(activitiProperties.getCustomMybatisXMLMappers()));
//        }
//
//        conf.setAsyncExecutor(springAsyncExecutor);
//
//        return conf;
//    }
//
//    @Bean
//    public SpringAsyncExecutor springAsyncExecutor(TaskExecutor taskExecutor) {
//        return new SpringAsyncExecutor(taskExecutor, springRejectedJobsHandler());
//    }
//
//    @Bean
//    public SpringRejectedJobsHandler springRejectedJobsHandler() {
//        return new SpringCallerRunsRejectedJobsHandler();
//    }
//
//    @Bean
//    @ConditionalOnMissingBean
//    public TaskExecutor taskExecutor() {
//        return new SimpleAsyncTaskExecutor();
//    }
//
//
//
//    protected String defaultText(String deploymentName, String deploymentName1) {
//        if (StringUtils.hasText(deploymentName))
//            return deploymentName;
//        return deploymentName1;
//    }
//
//    protected Set<Class<?>> getCustomMybatisMapperClasses(List<String> customMyBatisMappers) {
//        Set<Class<?>> mybatisMappers = new HashSet<>();
//        for (String customMybatisMapperClassName : customMyBatisMappers) {
//            try {
//                Class<?> customMybatisClass = Class.forName(customMybatisMapperClassName);
//                mybatisMappers.add(customMybatisClass);
//            } catch (ClassNotFoundException e) {
//                throw new IllegalArgumentException("Class " + customMybatisMapperClassName + " has not been found.", e);
//            }
//        }
//        return mybatisMappers;
//    }
//
//    public List<org.springframework.core.io.Resource> discoverProcessDefinitionResources(ResourcePatternResolver applicationContext, String prefix, List<String> suffixes, boolean checkPDs) throws IOException {
//        if (checkPDs) {
//
//            List<org.springframework.core.io.Resource> result = new ArrayList<>();
//            for (String suffix : suffixes) {
//                String path = prefix + suffix;
//                org.springframework.core.io.Resource[] resources = applicationContext.getResources(path);
//                if (resources.length > 0) {
//                    result.addAll(Arrays.asList(resources));
//                }
//            }
//
//            if (result.isEmpty()) {
//                logger.info("No process definitions were found for autodeployment");
//            }
//
//            return result;
//        }
//        return new ArrayList<>();
//    }
//}
